package com.yanqu.road.dao.impl.systemgroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.common.SystemServerGroupWeekData;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SystemServerGroupWeekDataDaoImpl extends TempDataObjectDao<SystemServerGroupWeekData> {


    public static String getSystemServerGroupWeekDataSql = "select * from `t_c_new_system_server_week_group` where `weekId`=?";
    public Map<Integer, List<SystemServerGroupWeekData>> getSystemServerGroupWeekData(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<SystemServerGroupWeekData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getSystemServerGroupWeekDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                SystemServerGroupWeekData data = parse(rs);
                if(!map.containsKey(data.getSystemId())){
                    map.put(data.getSystemId(), new ArrayList<>());
                }
                map.get(data.getSystemId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSystemServerGroupWeekDataSql = "update `t_c_new_system_server_week_group` set openDay=?,serverIdList=?,crossId=?,addTime=? where weekId=? and systemId=? and groupId=?";
    @Override
    protected boolean updateData(SystemServerGroupWeekData systemServerGroupWeekData) {
        systemServerGroupWeekData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getOpenDay()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(systemServerGroupWeekData.getServerIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getCrossId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(systemServerGroupWeekData.getAddTime().getTime())));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getSystemId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getGroupId()));
        if (getDb().executeNoneQuery(updateSystemServerGroupWeekDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSystemServerGroupWeekDataSql = "insert into `t_c_new_system_server_week_group`(weekId,systemId,groupId,openDay,serverIdList,crossId,addTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SystemServerGroupWeekData systemServerGroupWeekData) {
        systemServerGroupWeekData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getSystemId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getOpenDay()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(systemServerGroupWeekData.getServerIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, systemServerGroupWeekData.getCrossId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(systemServerGroupWeekData.getAddTime().getTime())));
        if (getDb().executeNoneQuery(insertSystemServerGroupWeekDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SystemServerGroupWeekData parseData(ResultSet rs) throws SQLException {
        SystemServerGroupWeekData systemServerGroupWeekData = new SystemServerGroupWeekData();
        systemServerGroupWeekData.setWeekId(rs.getInt("weekId"));
        systemServerGroupWeekData.setSystemId(rs.getInt("systemId"));
        systemServerGroupWeekData.setGroupId(rs.getInt("groupId"));
        systemServerGroupWeekData.setOpenDay(rs.getInt("openDay"));
        systemServerGroupWeekData.setServerIdList(JSON.parseObject(rs.getString("serverIdList"), new TypeReference<List<Long>>() {}));
        systemServerGroupWeekData.setCrossId(rs.getInt("crossId"));
        systemServerGroupWeekData.setAddTime(rs.getTimestamp("addTime"));

        return systemServerGroupWeekData;
    }
}
